//
//  NSAttributedString+LVAddition.m
//  BaiduTravelHD
//
//  Created by easy on 13-1-26.
//  Copyright (c) 2013年 Baidu Inc. All rights reserved.
//

#import "NSAttributedString+LVAddition.h"
#import <CoreText/CoreText.h>
@implementation NSAttributedString (LVAddition)
-(CGSize) sizeWithConstrainedToSize:(CGSize) size fitRange:(NSRange *) fitRange {
    CTFramesetterRef setter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)self);
    CFRange range;
    CGSize fitSize = CTFramesetterSuggestFrameSizeWithConstraints(setter, CFRangeMake(0, [self length]), NULL, size, &range);
    if (fitRange != NULL) {
        *fitRange = NSMakeRange(range.location, range.length);
    }
    CFRelease(setter);
    return fitSize;
}

-(CGSize) sizeWithConstrainedToSize:(CGSize) size fitRange:(NSRange *)fitRange Leading:(CGFloat)leading {
    CGSize fitSize = [self sizeWithConstrainedToSize:size fitRange:fitRange];
    int systemVersion = [[[UIDevice currentDevice]systemVersion]intValue];
    if (systemVersion == 4) {
        fitSize = CGSizeMake(fitSize.width, fitSize.height+leading);
    }
    return fitSize;
}

-(CGSize) sizeWithConstrainedToSize:(CGSize) size {
    return [self sizeWithConstrainedToSize:size fitRange:NULL];
}


-(CGSize) sizeWithConstrainedToSize:(CGSize) size Leading:(CGFloat)leading
{
    return [self sizeWithConstrainedToSizeWithLeading:size Leading:leading];
}

-(CGSize) sizeWithConstrainedToSizeWithLeading:(CGSize)size Leading:(CGFloat)leading{
    CGSize retSize = [self sizeWithConstrainedToSize:size];
    int systemVersion = [[[UIDevice currentDevice]systemVersion]intValue];
    if (systemVersion == 4) {
        retSize = CGSizeMake(retSize.width, retSize.height+leading);
    }
    return retSize;
}
@end
